home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / challenge / 13.08 / ChallengeEqEval.sit.hqx / Challenge, Equation Evaluator / Evaluate.h < prev    next >
Text File  |  1997-06-25  |  5KB  |  217 lines

  1. /*----------------------------------------------------------------------
  2.     Type definitions and function prototype from the Programmer's Challenge
  3.     problem statement.
  4. ----------------------------------------------------------------------*/
  5.  
  6. typedef unsigned long ulong;
  7.  
  8. typedef struct Values {
  9.     float    first;        //    first value in sequence
  10.     float    delta;        //    difference between consecutive values
  11.     ulong    howMany;    //    number of values
  12. } Values;
  13.  
  14. typedef struct IntValues {
  15.     long    first;        //    first value in sequence
  16.     long    delta;        //    difference between consecutive values
  17.     ulong    howMany;    //    number of values
  18. } IntValues;
  19.  
  20. typedef struct Results {
  21.     float    equationResult;    // result of equation, given x,y,n
  22.     float    x;                // value of x that produced equationResult
  23.     float    y;                // value of y that produced equationResult
  24.     long    n;                // value of n that produced equationResult
  25. } Results;
  26.  
  27. void Evaluate(
  28.     char            *equation,    // null-terminated equation to evaluate
  29.     const Values    *xP,        // input values for x
  30.     const Values    *yP,        // input values for y
  31.     const IntValues    *nP,        // input values for n
  32.     Results            w[]            // preallocated storage for equation values
  33. );
  34.  
  35.  
  36.  
  37. /*----------------------------------------------------------------------
  38.     These are types and function prototypes used internally
  39. ----------------------------------------------------------------------*/
  40.  
  41. extern ulong gVariableFlags;
  42. extern ulong gNumConstants;        // Number of constants found/stored
  43. extern float *gConstants;        // Array of constant values from equation
  44. extern ulong *gCodeBase;        // Address of start of generated code
  45. extern ulong *gNextInstruction;    // Where next generated instruction will be stored
  46.  
  47. union Constant {
  48.     float    f;            // a float constant
  49.     long    which;        // which one of a group (kVariable, kAddOp, etc.)
  50. };
  51. typedef union Constant Constant;
  52.  
  53. struct TokenNode {
  54.     int                    token;
  55.     Constant            value;
  56.     struct TokenNode    *left;
  57.     struct TokenNode    *right;        // unused for unary operators, functions
  58. };
  59. typedef struct TokenNode TokenNode;
  60.  
  61. //
  62. //    Return the next token in the equation.  Adjust the pointer to
  63. //    point just after the token.
  64. //
  65.  
  66. enum {
  67.     tokFloat,
  68.     tokLeftParen,
  69.     tokRightParen,
  70.     tokAddOp,
  71.     tokMulOp,
  72.     tokPowerOp,
  73.     tokNegate,
  74.     tokAbs,
  75.     tokRootOp,
  76.     tokFactorial,
  77.     tokFunction,    // ln, sin, tan, etc.
  78.     tokVariable,    // x, y, r, t, etc.
  79.     tokAssign,        // =
  80.     tokInvalid
  81. };
  82.  
  83. //    Indices for the various functions
  84. enum {
  85.     kFuncPower,
  86.     kFuncAbs,
  87.     kFuncSquareRoot,
  88.     kFuncFactorial,
  89.     kFuncExp,
  90.     kFuncLogN,
  91.     kFuncLogTen,
  92.     kFuncSin,
  93.     kFuncCos,
  94.     kFuncTan,
  95.     kFuncSec,
  96.     kFuncCsc,
  97.     kFuncCot,
  98.     kFuncArcSin,
  99.     kFuncArcCos,
  100.     kFuncArcTan,
  101.     kFuncArcSec,
  102.     kFuncArcCsc,
  103.     kFuncArcCot,
  104.     kFuncSinh,
  105.     kFuncCosh,
  106.     kFuncTanh,
  107.     kFuncSech,
  108.     kFuncCsch,
  109.     kFuncCoth,
  110.     kFuncAtan2
  111. };
  112.  
  113. //    Values of "which" for misc. tokens
  114. enum {
  115.     kFloat,
  116.     kInteger,
  117.     kLeftParen,
  118.     kRightParen,
  119.     kAdd,
  120.     kSubtract,
  121.     kMultiply,
  122.     kDivide,
  123.     kExponent,
  124.     
  125.     //    "variables"
  126.     kPi,
  127.     kE,
  128.     kR,
  129.     kTheta,
  130.     kX,
  131.     kY,
  132.     kZ,
  133.     kN,
  134.     
  135.     kEquals,
  136.     kInvalidToken
  137. };
  138.  
  139. //    Constants for bits in gVariableFlags
  140. enum {
  141.     kRMask        = 0x0001,        //    r was used in the equation
  142.     kThetaMask    = 0x0002,        //    t was used in the equation
  143.     kXMask        = 0x0004,        //    x was used in the equation
  144.     kYMask        = 0x0008,        //    y was used in the equation
  145.     kNMask        = 0x0010,        //    n was used in the equation
  146.     kEMask        = 0x0020,        //    e was used in the equation
  147.     kPiMask        = 0x0040,        //    p was used in the equation
  148.     kFunctionOfY= 0x0080        //    function was of the form "z="
  149. };
  150.  
  151. //    The various integer and floating point registers used
  152. enum {
  153.     regR0            = 0,        //    used for function calls
  154.     regTOC            = 2,        //    TOC is pointer to globals
  155.     regFunctionGlue = 12,
  156.     regVariableBase    = 13,        //    points to (temporary) values stored in memory
  157.     regFunctionBase = 14,        //    points to array of function pointers
  158.     regSavedLR        = 15,        //    we save caller's LR here
  159.     regXValues        = 16,
  160.     regYValues        = 17,
  161.     regNValues        = 18,
  162.     regNIntValues    = 19,
  163.     regResults        = 20,
  164.     regNInteger        = 21,
  165.     regXCount        = 22,
  166.     regYCount        = 23,
  167.     regNCount        = 24,
  168.     
  169.     fpResult    = 1,            //    results always in fp1
  170.     fpArg1        = 1,            //    first fp argument in fp1
  171.     fpArg2        = 2,            //    second fp argument in fp2
  172.     fpTemp1        = 3,
  173.     fpTemp2        = 4,
  174.     fpTempResult= 5,
  175.     
  176.     regIntTemp1    = 4,            //    a volatile integer register
  177.     regFreeBase    = 14,            //    first non-volatile register
  178.     regMax        = 32,            //    number of fp registers
  179.     
  180.     regN        = 25,
  181.     regX        = 26,
  182.     regY        = 27,
  183.     regR        = 28,
  184.     regTheta    = 29,
  185.     regE        = 30,
  186.     regPi        = 31,
  187.  
  188.     regMemory    = 100            // fake register number for memory locations
  189. };
  190.  
  191. //    Token recognition routines
  192. int GetToken(char **s, Constant *value);
  193. int GetNumberToken(char **s, Constant *value);
  194.  
  195. //    Token memory management
  196. TokenNode *NewTokenNode(TokenNode *left, TokenNode *right, int token);
  197. void FreeTree(TokenNode *root);
  198.  
  199. //    Routines to parse various productions in the grammar
  200. TokenNode *ParseExpression(char **s);
  201. TokenNode *ParseTerm(char **s);
  202. TokenNode *ParseFactor(char **s);
  203. TokenNode *ParseNegative(char **s);
  204. TokenNode *ParseFactorial(char **s);
  205. TokenNode *ParseSimpleValue(char **s);
  206. TokenNode *ParseStatement(char **s);
  207.  
  208. //    Routines for creating and calling generated PowerPC code
  209. void InitCodeGen(ulong *codeStart);
  210. int CodeGenTree(TokenNode *root, int resultRegister);
  211. void CodeGen(TokenNode *root);
  212. float CallGeneratedCode(void *addr, const Values *xValues, const Values *yValues,
  213.                         const IntValues *nIntValues, const Values *nValues,
  214.                         Results *resultsPtr,
  215.                         float *constants, void *functions,
  216.                         float e, float pi);
  217.